package com.chahua.organized.annotion;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author chahua
 * @date 2022/4/9 22:17
 **/
public class AllowConstraintValidator implements ConstraintValidator<AllowValue, Integer> {
    Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(AllowValue constraintAnnotation) {
        int[] vals = constraintAnnotation.vals();
        for (int value : vals) {
            set.add(value);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return value == null || set.contains(value);
    }
}
